package UseIO.UseSerialize;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Serialize {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //input();
        //someOutput();
        someInput();
    }
    public static void output() throws IOException{
        // 序列化输出
        Student student = new Student("小美",18);
        ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("8.内置模块/UseIO/serialize.txt"));
        oo.writeObject(student);
        oo.close();
    }
    public static void input() throws IOException, ClassNotFoundException {
        // 反序列化
        ObjectInputStream os = new ObjectInputStream(new FileInputStream("8.内置模块/UseIO/serialize.txt"));
        Object o = os.readObject();
        System.out.println(o);
        os.close();
    }

    public static void someOutput() throws IOException {
        Student s1 = new Student("小红", 18);
        Student s2 = new Student("小蓝", 19);
        Student s3 = new Student("小黄", 20);

        ArrayList<Student> arrStudent = new ArrayList<>();
        Collections.addAll(arrStudent,s1,s2,s3);

        ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("8.内置模块/UseIO/someSerialize.txt"));
        oo.writeObject(arrStudent);
        oo.close();
    }

    public static void someInput() throws IOException, ClassNotFoundException {
        ObjectInputStream os = new ObjectInputStream(new FileInputStream("8.内置模块/UseIO/someSerialize.txt"));

        ArrayList<Student> o = (ArrayList<Student>) os.readObject();
        for (Student student : o) {
            System.out.println(student);
        }
        os.close();
    }

}
